package com.hao.tool.web.haotool;

import cn.hutool.core.io.FileUtil;
import com.hao.tool.common.util.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.util.*;

/**
 * description
 *
 * @author RuiHaoZ on CMHK.jx
 * @date 2020-07-04 19:33
 */
public class BatchUploadFileUtil {

    @Test
    public void test() {
        List<File> fileList = FileUtil.loopFiles("/Users/haol/Documents/Personal/17sucai");
        fileList.forEach(this::uploadFile);
    }


    @Test
    public void tes2t() {
        List<File> fileList = FileUtil.loopFiles("/Users/haol/Documents/Personal/17sucai");
        Map<String, File> fileMap = new HashMap<>();
        fileList.forEach(f -> {
            fileMap.put(f.getName(), f);
        });
        Set<String> stringSet = new HashSet<>();
        String content = FileUtil.readUtf8String("/Users/haol/develop/hao-tool-web/addFile2.json");
        for (String s : content.split("\n")) {
            if (StringUtils.isNotBlank(s)) {
                if (s.contains("500,0") || s.contains("Read timed out")) {
                    stringSet.add(s.substring(0, s.lastIndexOf("->")));
                }
            }
        }
        for (String s : stringSet) {
            File file = fileMap.get(s);
            uploadFile(file);
        }

    }

    public void uploadFile(File f) {
        String response = "";

        if (f.getName().contains(".zip") || f.getName().contains("rar")) {
            Map<String, File> fileMap = new HashMap<>();
            fileMap.put("file", f);
            try {
                response = HttpClientUtil.build().setTimeOut(60000 * 2)
                        .sendPost("http://lh:8087/rest/template/add", new HashMap<>(), fileMap);
            } catch (Exception e) {
                response = e.getMessage();
            }
        } else {
            response = "not";
        }
        String resultFilePath = "/Users/haol/develop/hao-tool-web/addFile2.json";
        FileUtil.appendUtf8String(f.getName() + "->" + response + "\n", new File(resultFilePath));

    }


    @Test
    public void test3() {
        String s = "5ef0040ae3409.zip->Read timed out";
        System.out.println(s.substring(0, s.lastIndexOf("->")));

    }




}
